//
//  ReceiverDBModel.m
//  TengyunStation
//
//  Created by Johnny on 15/8/24.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "ReceiverDBModel.h"
#import "SocketMethod.h"
#import "Singleton.h"

@implementation ReceiverDBModel

- (void)saveReceiverDBModel {
    ReceiverDBModel *receiver = [ReceiverDBModel findFirstByCriteria:[NSString stringWithFormat:@"WHERE telephone = '%@' AND websiteId = '%@'",self.telephone,[Singleton shareInstance].userMessage.websiteId]];
    self.saveday = [self todayTime];
    self.websiteId = [Singleton shareInstance].userMessage.websiteId;
    if (receiver == nil) {
        [super save];
    } else {
        self.pk = receiver.pk;
        [self saveOrUpdate];
    }
}

- (NSString *)todayTime {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    return [dateFormatter stringFromDate:[NSDate date]];
}

- (id)initWithString:(NSString *)string {
    self = [super init];
    if (self) {
        NSArray *array = [string componentsSeparatedByString:@","];
        self.name = [SocketMethod subArray:array Index:0];
        self.telephone = [SocketMethod subArray:array Index:1];
        NSString *address = [SocketMethod subArray:array Index:2];
        if ([address rangeOfString:@"#"].location != NSNotFound) {
            NSArray *addressArray = [address componentsSeparatedByString:@"#"];
            self.floor = [SocketMethod subArray:addressArray Index:0];
            self.room = [SocketMethod subArray:addressArray Index:1];
        } else {
            self.room = address;
        }
        self.websiteId = [Singleton shareInstance].userMessage.websiteId;
    }
    return self;
}

@end
